package com.sharp.authorization.controller.admin;

import com.sharp.authorization.annotation.auth.AdminAuthRole;
import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.service.admin.AdminService;
import com.sharp.authorization.service.permit.AuthRoleService;
import com.sharp.authorization.service.permit.PermitCoreService;
import com.sharp.authorization.vo.permit.BindUserRoleReq;
import com.sharp.authorization.vo.role.RoleRes;
import com.sharp.authorization.vo.user.*;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.vo.common.CommonResVO;
import com.sharp.kernel.vo.query.QueryRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * Title: AdminUserController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/4/9 18:01
 * @description Project Name: Mall
 * @Package: com.srct.service.account.controller.admin
 */
@ApiIgnore
@Auth(AdminAuthRole.class)
@Api(value = "用户管理", tags = "后台管理员")
@RestController("AdminUserController")
@RequestMapping(value = "/admin/user")
public class AdminUserController {

    @Resource
    private AdminService adminService;
    @Resource
    private PermitCoreService permitCoreService;
    @Resource
    private AuthRoleService authRoleService;

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity<Response<CommonResVO>> addUser(@RequestBody AddUserReq req) {
        adminService.addUser(req);
        return ResponseHandler.commonResponse();
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<Response<CommonResVO>> updateUser(@RequestBody UpdateUserReq req) {
        adminService.updateUser(req);
        return ResponseHandler.commonResponse();
    }

    @ApiOperation(value = "获取所有用户", notes = "获取所有用户")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Response<QueryRespVO<QueryUserRes>>> queryUser(QueryUserReq req) {
        QueryRespVO<QueryUserRes> res = adminService.queryUser(req);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "获取用户角色列表", notes = "系统管理员准入")
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public ResponseEntity<Response<List<RoleRes>>> getUserRole(String userId) {
        List<RoleRes> res = authRoleService.getRoleListByUserId(userId);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "绑定用户角色", notes = "系统管理员准入")
    @RequestMapping(value = "/role/bind", method = RequestMethod.POST)
    public ResponseEntity<Response<CommonResVO>> bindUserRole(@RequestBody BindUserRoleReq req) {
        permitCoreService.bindUserRole(req);
        return ResponseHandler.commonResponse();
    }

    @ApiOperation(value = "克隆用户角色", notes = "系统管理员准入")
    @RequestMapping(value = "/role/clone", method = RequestMethod.POST)
    public ResponseEntity<Response<CommonResVO>> cloneUserRole(CloneUserRoleReq req) {
        adminService.cloneUserRole(req);
        return ResponseHandler.commonResponse();
    }
}
